package com.xy.hms.config;

import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author xiaye
 * @create 2020-02-20-13:24
 * <p>
 * 拓展mvc
 */

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    /**
     * 第一种方式
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/404").setViewName("404");
        registry.addViewController("/500").setViewName("500");
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/login").setViewName("login");

        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");

        registry.addViewController("/login.html").setViewName("login");
        registry.addViewController("/lock_screen.html").setViewName("lock_screen");
        registry.addViewController("/add_room.html").setViewName("room/add_room");
        registry.addViewController("/add_staff.html").setViewName("staff/add_staff");
        registry.addViewController("/sign_up.html").setViewName("sign_up");
        registry.addViewController("/new_booking.html").setViewName("booking/new_booking");
        registry.addViewController("/view_booking.html").setViewName("booking/view_booking");
        registry.addViewController("/user_profile.html").setViewName("/user/user_profile");
        registry.addViewController("/edit_staff.html").setViewName("/staff/edit_staff");

    }


    /**
     * 第二种方式
     * @return
     */
//    @Bean
//    WebMvcConfigurer webMvcConfigurer(){
//        WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
//            @Override
//            public void addViewControllers(ViewControllerRegistry registry) {
//                registry.addViewController("/").setViewName("login");
//                registry.addViewController("/login").setViewName("login");
//                registry.addViewController("/index").setViewName("index");
//                registry.addViewController("/all_staffs").setViewName("all_staffs");
//                registry.addViewController("/all_rooms").setViewName("all_rooms");
//                registry.addViewController("/add_room").setViewName("add_room");
//                registry.addViewController("/add_staff").setViewName("add_staff");
//
//            }
//        };
//        return webMvcConfigurer;
//    }


    /**
     * 添加自己定义的区域信息解析器
     * @return
     */
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolve();
    }


    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerIntercepter())
                .addPathPatterns("/**")  //拦截所有请求
                .excludePathPatterns("/","/login","/login.html","/sign_up.html","/user","/user/register","/myservlet"
                        , "/assets/css/**","/assets/js/**","/assets/img/**","/assets/plugins/**");
    }





}
